package com.example.experiment4

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.experiment4.databinding.ActivityEditInfoBinding

class EditInfoActivity : AppCompatActivity() {
    private lateinit var binding: ActivityEditInfoBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityEditInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 预填数据
        val info = PlayerRepository.getInfo()
        binding.etName.setText(info.name)
        binding.etPhone.setText(info.phone)
        when (info.gender) {
            Gender.MALE -> binding.rbMale.isChecked = true
            Gender.FEMALE -> binding.rbFemale.isChecked = true
            Gender.UNKNOWN -> binding.rbUnknown.isChecked = true
        }

        binding.btnCancel.setOnClickListener { finish() }
        binding.btnSave.setOnClickListener {
            val name = binding.etName.text?.toString()?.trim().orEmpty()
            val phone = binding.etPhone.text?.toString()?.trim().orEmpty()
            val gender = when (binding.rgGender.checkedRadioButtonId) {
                binding.rbMale.id -> Gender.MALE
                binding.rbFemale.id -> Gender.FEMALE
                else -> Gender.UNKNOWN
            }
            PlayerRepository.update(name, gender, phone)
            setResult(RESULT_OK)
            finish()
        }
    }
}


